﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Data.SqlClient;

namespace TrainManage.Controllers
{
    public class AuditController : Controller
    {
        //
        // GET: /Audit/
        /// <summary>
        /// 审核
        /// </summary>
        /// <returns></returns>
        RepairsBll rep = new RepairsBll();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult MyAudit(int PageIndex=1)
        {
            int PageSize = 5;
            int PageCount = 0;
            List<Repairs> replist=new List<Repairs>();
            if (Convert.ToInt32( Session["RoleId"] )== 4)
            {
                SqlParameter sp = new SqlParameter();
                sp.ParameterName = "ExamineState";
                sp.Value = "0";
              var list= rep.ShowPair(PageIndex, PageSize, out PageCount, sp);
              ViewBag.list = list;
              ViewBag.TotalPage = (int)Math.Ceiling((double)PageCount / (double)PageSize);
              ViewBag.PageIndex = PageIndex;
              ViewBag.PageSize = PageSize;
            }
            else
            {
                SqlParameter sp = new SqlParameter();
                sp.ParameterName = "ExamineState";
                sp.Value = "2";
                var list = rep.ShowPair(PageIndex, PageSize, out PageCount, sp);
                ViewBag.list = list;
                ViewBag.TotalPage = (int)Math.Ceiling((double)PageCount / (double)PageSize);
                ViewBag.PageIndex = PageIndex;
                ViewBag.PageSize = PageSize;
            }
            return View();
        }
        public ActionResult Audit(int id)
        {
            Repairs repa = rep.BackPairs(id);

            return View(repa);
        }
        public int Upd()
        {
            int id = Convert.ToInt32(Request["RepairsId"]);
            string choice = Request["choice"];
            Repairs repa = rep.BackPairs(id);
            repa.choice = choice;
            if (Convert.ToInt32(Session["Roleid"]) == 4)
            {
                repa.ExamineState = 2;
            }
            else if (Convert.ToInt32(Session["Roleid"]) == 5)
            {

                repa.ExamineState = 3;
            }
            if (rep.UpdPairs(repa, id))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public bool Upd1()
        {
            int id = Convert.ToInt32(Request["RepairsId"]);
            Repairs repa = rep.BackPairs(id);
            repa.ExamineState = 1;
            if (rep.UpdPairs(repa, id))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public ActionResult Search()
        {
            string name = Request["RepairsName"];
            string Dates = Request["txtDate"];

            SqlParameter sp = new SqlParameter();
            sp.ParameterName = "RepairsName";
            sp.Value = name;
            SqlParameter sp1 = new SqlParameter();
            sp1.ParameterName = "FindDate";
            sp1.Value = Dates;

            List<Repairs> replist = rep.ShowPair(sp, sp1);
            ViewBag.list=replist;
            return View("MyAudit");
        }
    }
}
